package com.example.lizhixin.object_three_team.Fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.widget.EditText;

import com.example.lizhixin.object_three_team.Base.BaseFragment;
import com.example.lizhixin.object_three_team.Bean.CommuntiyBean;
import com.example.lizhixin.object_three_team.R;
import com.example.lizhixin.object_three_team.Uitls.CommunityViewAdapter;
import com.example.lizhixin.object_three_team.Uitls.ToastUtils;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunityFragment extends BaseFragment {
    private EditText EditSearch;
    private RecyclerView RvCommunityList;
    private List<CommuntiyBean.DataBean> data = new ArrayList<>();
    private CommunityViewAdapter adapter;
    private CommuntiyBean communtiyBean;
    private List<CommuntiyBean.DataBean> communtiyBeanData;

    public CommunityFragment() {
        // Required empty public constructor
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_community;
    }

    @Override
    protected void initView() {
        RvCommunityList = (RecyclerView) getActivity().findViewById(R.id.rv_community_list);
        EditSearch = (EditText) getActivity().findViewById(R.id.edit_search);


        StaggeredGridLayoutManager recyclerViewLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        RvCommunityList.setLayoutManager(recyclerViewLayoutManager);


        OkHttpUtils
                .get()
                .url("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtils.showShortToast(getActivity(), "错误：" + e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        communtiyBean = gson.fromJson(response, CommuntiyBean.class);
                        communtiyBeanData = communtiyBean.getData();
                        data.addAll(communtiyBeanData);
                        Log.e("cq11", data + "");
                        init();
                    }
                });


    }

    private void init() {
        adapter = new CommunityViewAdapter(getActivity(), data);
        RvCommunityList.setAdapter(adapter);

        adapter.setOnRecyclerViewItemClickListener(new CommunityViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {

            }
//            @Override
//            public void onLongClick(int position) {
//                ToastUtils.showShortToast(getActivity(), "长第" + position);
//            }
        });
    }

    @Override
    protected void initData() {

    }


}
